package Controller;



import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import Model.*;
import View.*;

/*
* Handles all the button input and is passed the ai that menuhandler uses
* -Commented(Apr 4,2011)
* Samual Martelli
* </pre>
*/
public class ButtonHandler implements ActionListener {

	private GraphicalView view;
	private GameButton buttonHandled;
	private int gameType;
	private GameAI myGameAI;//needs the other controller to pass on the playing position
	public ButtonHandler(GameButton button,GraphicalView curView,GameAI ai){
		 buttonHandled = button;
		 view = curView;
		 myGameAI = ai;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// checks the source of the button and attempt to play the game
		 GameButton buttonPressed = (GameButton) e.getSource();
		Point attemptPlayHere = new Point();
	     attemptPlayHere = buttonPressed.getCoord();
	     //get players current position
	     int px = 0;//player's x
	     int py =0; //player's y
	     px = myGameAI.getGame().getData().getPlayerOfGame().getCoordinateX();
	     py = myGameAI.getGame().getData().getPlayerOfGame().getCoordinateY();
	     //player movement
	     if(px == attemptPlayHere.x && (py -1) == attemptPlayHere.y){
	    	 gameType = myGameAI.getGame().getGameType();
	    	 switch(gameType){
	    	 case Game.PACMAN_GAME:
	    		 myGameAI.movePlayer(GameAI.UP);
	    		 break;
	    	 case Game.BUNNY_GAME:
	    		 myGameAI.movePlayer(GameAI.UP);
	    		 break;
	    	 case Game.MOUSE_GAME:
	    		 myGameAI.movePlayer(GameAI.UP);
	    		 break;
	    	 }
	     }
	     else if(px == attemptPlayHere.x && (py  + 1) == attemptPlayHere.y){
	    	 gameType = myGameAI.getGame().getGameType();
	    	 switch(gameType){
	    	 case Game.PACMAN_GAME:
	    		 myGameAI.movePlayer(GameAI.DOWN);
	    		 break;
	    	 case Game.BUNNY_GAME:
	    		 myGameAI.movePlayer(GameAI.DOWN);
	    		 break;
	    	 case Game.MOUSE_GAME:
	    		 myGameAI.movePlayer(GameAI.DOWN);
	    		 break;
	    	 }
	     }
	     else if((px+1) == attemptPlayHere.x  && py == attemptPlayHere.y){
	    	 gameType = myGameAI.getGame().getGameType();
	    	 switch(gameType){
	    	 case Game.PACMAN_GAME:
	    		 myGameAI.movePlayer(GameAI.RIGHT);
	    		 break;
	    	 case Game.BUNNY_GAME:
	    		 myGameAI.movePlayer(GameAI.RIGHT);
	    		 break;
	    	 case Game.MOUSE_GAME:
	    		 myGameAI.movePlayer(GameAI.RIGHT);
	    		 break;
	    	 }
	     }
	     else if((px-1) == attemptPlayHere.x && py == attemptPlayHere.y){
	    	 gameType = myGameAI.getGame().getGameType();
	    	 switch(gameType){
	    	 case Game.PACMAN_GAME:
	    		 myGameAI.movePlayer(GameAI.LEFT);
	    		 break;
	    	 case Game.BUNNY_GAME:
	    		 myGameAI.movePlayer(GameAI.LEFT);
	    		 break;
	    	 case Game.MOUSE_GAME:
	    		 myGameAI.movePlayer(GameAI.LEFT);
	    		 break;
	    	 }
	     }
		for(Monster m:myGameAI.getGame().getData().getMonsters()){
			Point old = m.getCoordinate();
			view.changeButtonIcon(old.x, old.y, GraphicalView.noIcon);
			myGameAI.moveMonster(m.getMonsterType(), m);
		}
		if(myGameAI.isGameOver()){
			view.gameOverMessage();
		}
		
	}
}
